import { createApp } from 'vue'
import './assets/less/reset.less'
import App from './App.vue'
import router from './router/index.js'
import {createPinia} from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import './mock/mock.js'
import { useUserInfoStore } from '@/store/userInfo.js'

const isRoute = to =>{
  return router.getRoutes().filter(item=>item.path === to.path).length>0;
}

router.beforeEach((to, from, next) => {
  if(to.path !== '/login' && !userInfoStore.token){
    next('/login')
  }
  if(!isRoute(to)){
    next('/404')
  }
  next()
})

const app = createApp(App)

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
const pinia = createPinia()
app.use(ElementPlus).use(pinia);
//必须在pinia实例化之后调用
const userInfoStore = useUserInfoStore();
//必须在路由实例化创建前调用
userInfoStore.addMenu(router,'refresh')

app.use(router)
app.mount('#app')

